package edu.tgc.service.file;

import java.util.Collection;
import java.util.Date;
import java.util.UUID;

import org.abyss.common.Constants;
import org.abyss.common.annotation.Log;
import org.abyss.common.exception.CreateFaildServiceException;
import org.abyss.common.exception.UpdateFaildServiceException;
import org.abyss.common.service.CommonCrudService;
import org.abyss.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.tgc.entity.file.FileBidding;
import edu.tgc.security.SessionUserUtils;

@Log(value = "标书")
@Service
public class FileBiddingService extends CommonCrudService<FileBidding, UUID> {


    public FileBidding audit(UUID id, boolean state, String description) {
        FileBidding entity = assertExists(id);
        try {
            entity.setAuditTime(new Date());
            entity.setAuditUser(SessionUserUtils.getSysUser());
            entity.setDescription(description);
            entity.setAudit(state);
            entity = this.baseRepository.save(entity);
            this.baseRepository.flush();
            return entity;
        } catch (Throwable e) {
            throw new UpdateFaildServiceException(this.title + "审核失败", e);
        }
    }

    @Override
    @Log(value = Constants.Log.CREATE)
    @Transactional
    public FileBidding create(FileBidding model, Collection<String> ignorePropertiesArray) {
        FileBidding entity = assertNew(model);
        try {
            preCreate(model, entity);
            BeanCopyUtils.copyPropertiesIgnore(model, entity, ignorePropertiesArray);
            entity = baseRepository.save(entity);
            entity = baseRepository.save(entity);
            baseRepository.flush();
            afterCreate(model, entity);
            return entity;
        } catch (Throwable e) {
            throw new CreateFaildServiceException(title + Constants.Log.CREATE + "失败", e);
        }
    }

}
